package com.rzico.sbl.ui.order

import android.text.InputFilter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityOrderConfirmBinding
import com.rzico.sbl.model.CommonData
import com.rzico.sbl.viewmodel.OrderViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.ActivityStack
import com.xinnuo.common.other.DecimalNumberFilter
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.utils.showActionDialog
import kotlin.text.orEmpty

class OrderConfirmActivity : BaseActivity(R.layout.activity_order_confirm) {

    private val mBinding by lazy { ActivityOrderConfirmBinding.bind(getRootView()) }
    private val mPayList = ArrayList<CommonData>()
    private var mPluginId = ""

    override fun getViewModel() = get<OrderViewModel>()

    override fun initData() {
        initTitle(title = "确认订单")
    }

    override fun initListener() {
        mBinding.apply {
            // 初始页面
            confirmReal.filters = arrayOf<InputFilter>(DecimalNumberFilter())
            intent.also {
                confirmTotal.text = it.getExtra("amount")
                confirmNum.setRightString(it.getExtra("orderSn"))
                confirmTime.setRightString(it.getExtra("time"))
            }

            confirmPay.oneClick {
                mPayList
                    .isNotEmpty()
                    .conditionElse(
                        actionTrue = ::showActionDialog,
                        actionFalse = { getViewModel().getPayment() }
                    )
            }

            btConfirm.oneClick {
                val mRealMoney = confirmReal.content

                if (mPluginId.isEmpty()) {
                    showToast("请选择付款方式")
                    return@oneClick
                }

                if (mRealMoney.isEmpty()) {
                    showToast("请输入实付金额")
                    return@oneClick
                }

                getViewModel()
                    .getPaySN(
                        intent.getExtra("orderSn"),
                        mRealMoney,
                        confirmProof.content
                    ) { result ->
                        getViewModel()
                            .confirmOrder(
                                result,
                                mPluginId
                            ) {
                                showToast("确认订单成功！")

                                intent
                                    .getIntExtra("index", -1)
                                    .also {
                                        if (it > -1) {
                                            // 更新订单列表
                                            LiveEventBus
                                                .get("orderList")
                                                .post("refresh")
                                        }
                                    }

                                // 更新订单详情
                                LiveEventBus
                                    .get("orderDetail")
                                    .post("refresh")

                                ActivityStack.screenManager.popActivities(
                                    OrderDetailActivity::class.java,
                                    OrderConfirmActivity::class.java
                                )
                            }
                    }
            }
        }

        getViewModel()
            .mPayEvent
            .observe(getLifecycleOwner()) {
                mPayList.addItems(it)
                showActionDialog()
            }
    }

    private fun showActionDialog() {
        val items = mPayList.map { it.name.orEmpty() }.toTypedArray()
        showActionDialog(*items) { index, hint ->
            mPluginId = mPayList[index].paymentPluginId
            mBinding.confirmPay.setRightString(hint)
        }
    }

}